﻿[assembly: System.Web.UI.WebResource("BM.Tools.WebControls.MForm.Controls.ValueHandlers.LabelValueHandler.js", "application/x-javascript")]
[assembly: System.Web.UI.WebResource("BM.Tools.WebControls.MForm.Controls.ValueHandlers.LabelValueHandler.debug.js", "application/x-javascript")]

namespace BM.Tools.WebControls.MForm.Controls.ValueHandlers
{
    using System.Web.UI;
    using System.Web.UI.HtmlControls;
    using System.Web.UI.WebControls;

    using AjaxControlToolkit;
    using BM.Tools.WebControls.MForm.Controls.Items;

    /// <summary>
    /// A control that binds the <see cref="LeafItem" /> control with a <see cref="Label" /> control.
    /// </summary>
    [ClientScriptResource("BM.Tools.WebControls.MForm",
            "BM.Tools.WebControls.MForm.Controls.ValueHandlers.LabelValueHandler.js")]
    public class LabelValueHandler : StandardWebControlValueHandler<Label>
    {
        private HtmlInputHidden hiddenInput;

        /// <summary>
        /// Gets or sets Label.
        /// </summary>
        /// <value>
        /// The label.
        /// </value>
        [PersistenceMode(PersistenceMode.InnerProperty)]
        public Label Label
        {
            get
            {
                return Control;
            }

            set
            {
                Control = value;
            }
        }

        /// <summary>
        /// Gets or sets Value.
        /// </summary>
        public override string Value
        {
            get
            {
                return HiddenInput.Value;
            }

            set
            {
                HiddenInput.Value = value;
                Label.Text = value;
            }
        }

        /// <summary>
        /// Gets HiddenInput.
        /// </summary>
        protected HtmlInputHidden HiddenInput
        {
            get
            {
                if (hiddenInput == null)
                {
                    hiddenInput = new HtmlInputHidden();
                    hiddenInput.Attributes.Add("class", "HiddenInput");
                }

                return hiddenInput;
            }
        }

        /// <summary>
        /// Adds the control and the hidden input to the leaf item
        /// </summary>
        protected override void CreateChildControls()
        {
            Controls.Add(HiddenInput);
            base.CreateChildControls();
        }
    }
}